﻿using MediatR;
using TraceabilityApp_Api.Application.Command.Slaughter_屠宰_;
using TraceabilityApp_Api.DTO;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Slaughter_屠宰_
{
    public class GetCompanyHandler : IRequestHandler<GetCompanyCommand, ApiResult<CompanyDTO>>
    {
        private readonly IBase<CompanyModel> companyRepo;

        public GetCompanyHandler(IBase<CompanyModel> companyRepo)
        {
            this.companyRepo = companyRepo;
        }
        public Task<ApiResult<CompanyDTO>> Handle(GetCompanyCommand request, CancellationToken cancellationToken)
        {
            ApiResult<CompanyDTO> result = new ApiResult<CompanyDTO>();
            var company = companyRepo.GetAll().Where(x => !x.IsDeleted);
            if (!string.IsNullOrEmpty(request.Batch))
            {
                company=company.Where(x =>x.Batch.Contains(request.Batch));
            }
            var linq = (from a in company
                       select new CompanyDTO
                       {
                           Batch = a.Batch,
                           CityName = a.CityName,
                           CompanyId = a.CompanyId,
                           CompanyName = a.CompanyName,
                           CompanyProfile = a.CompanyProfile,
                           CountyName = a.CountyName,
                           EstablishTime = a.EstablishTime,
                           ProvinceName = a.ProvinceName
                       }).FirstOrDefault(x=>x.Batch==request.Batch);
            result.Data = linq;
            result.Code = ApiEnum.OK;
            result.Msg = "屠宰企业信息查询成功";
            return Task.FromResult(result);
        }
    }
}
